package com.jlastu.androidsx.ui.merchant;

import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;
import com.jlastu.androidsx.R;
import com.jlastu.androidsx.bean.OrderBean;
import com.jlastu.androidsx.bean.ShopAddress;
import com.jlastu.androidsx.bean.User;
import com.jlastu.androidsx.database.OrderBeanDBUtils;
import com.jlastu.androidsx.database.ShopAddressDBUtils;
import com.jlastu.androidsx.database.UserDBUtils;
import com.jlastu.androidsx.databinding.ActivityMOrderBinding;
import com.jlastu.androidsx.utils.NotificationHelper;

import org.greenrobot.eventbus.EventBus;

import cn.project.base.baseui.BaseNormalActivity;

public class MerchatGoodsDetailActivity extends BaseNormalActivity<ActivityMOrderBinding> {

    OrderBean order;
    long id = 0;
    String address_id ="";
    ShopAddress shopAddress=null;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_m_order;
    }

    @Override
    protected void initView() {
        mBinding.toolbar.tvTitle.setText("订单详情");
        mBinding.toolbar.rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void initData() {
        id = getIntent().getLongExtra("id",0);
        address_id = getIntent().getStringExtra("address_id");
        order = OrderBeanDBUtils.getInstance(getApplicationContext()).getOrderById(id);
        if (order!=null){
            Glide.with(this).load(order.getUrl()).into(mBinding.image);
            mBinding.tvGoodsTitle.setText(order.getTitle());
            mBinding.tvDes.setText(order.getContent());
            mBinding.tvType.setText(order.getGoods_type());
            mBinding.tvAddress.setText("产地："+order.getAddress());
            mBinding.tvDetailAddress.setText("详细地址："+order.getDetail_address());
            mBinding.tvPrice.setText("¥："+order.getPrice());
            shopAddress = ShopAddressDBUtils.getInstance(getApplicationContext()).getShopAddress(address_id);
            mBinding.tvUserAddress.setText("买家收货地址："+shopAddress.getAddress());
            User user = UserDBUtils.getInstance(getApplicationContext()).select(order.getUser_id());
            if (user!=null){
                mBinding.llUser.setVisibility(View.VISIBLE);
                mBinding.tvName.setText("买家昵称："+user.getName());
                if (!TextUtils.isEmpty(user.getPhone())){
                    mBinding.tvPhone.setText("买家联系方式："+user.getPhone());
                }

            }
        }
        if (!TextUtils.isEmpty(address_id)){
            mBinding.btnAdd.setVisibility(View.VISIBLE);
        }

        mBinding.btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                order.setState("1");
                OrderBeanDBUtils.getInstance(getApplicationContext()).change(getApplicationContext(),order);
                showToast("发货成功");
                NotificationHelper.show(getApplicationContext(),"发货成功！！！");
                EventBus.getDefault().postSticky("已发货");
                finish();
            }
        });
    }
}
